<?php
namespace WechatSDK\Components;

use WechatSDK\Lib\Tools;

require_once('Common.php');

/**
 * 客服账号管理组件
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class CustomAccount extends Common
{
    /** 多客服相关地址 */
    const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?';
    const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?';
    const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?';
    const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?';
    const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?';
    const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?';
    const CS_KF_ACCOUNT_INVITEWORKER_URL = '/customservice/kfaccount/inviteworker?';


    const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?';
    const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?';
    const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?';
    const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?';
    const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?';
    const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?';

    function __construct($appid, $secret, $token)
    {
        parent::__construct($appid, $secret, $token);
    }

    function __destruct()
    {
    }

    /**
     * 添加客服账号
     *
     * @param string $account 完整客服账号(账号前缀@公众号微信号，账号前缀最多10个字符)
     * @param string $nickname 客服昵称，最长6个汉字或12个英文字符
     * @param string $password 客服账号明文登录密码，会自动加密
     * @return bool|array
     */
    public function addAccount($data)
    {
        $this->throwAccessTokenError();
        $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_ADD_URL . "access_token={$this->access_token}", Tools::json_encode($data));
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 修改客服账号信息
     *
     * @param string $account //完整客服账号，格式为：账号前缀@公众号微信号，账号前缀最多10个字符，必须是英文或者数字字符
     * @param string $nickname //客服昵称，最长6个汉字或12个英文字符
     * @param string $password //客服账号明文登录密码，会自动加密
     * @return bool|array
     * 成功返回结果
     * {
     *   "errcode": 0,
     *   "errmsg": "ok",
     * }
     */
    public function updateAccount($data)
    {
        $this->throwAccessTokenError();
        $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPDATE_URL . "access_token={$this->access_token}", Tools::json_encode($data));
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 删除客服账号
     * @param string $account 完整客服账号(账号前缀@公众号微信号，账号前缀最多10个字符)
     * @return bool|array
     */
    public function deleteAccount($account)
    {
        $this->throwAccessTokenError();
        $result = Tools::httpGet(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_DEL_URL . "access_token={$this->access_token}" . '&kf_account=' . $account);
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 上传客服头像
     * @param string $account 完整客服账号(账号前缀@公众号微信号，账号前缀最多10个字符)
     * @param string $imgfile 头像文件完整路径,如：'D:\user.jpg'。头像文件必须JPG格式，像素建议640*640
     * @return bool|array
     */
    public function setAccountImg($account, $imgfile)
    {
        $this->throwAccessTokenError();
        $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL . "access_token={$this->access_token}" . '&kf_account=' . $account, array('media' => '@' . $imgfile), true);
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 获取多客服客服基本信息
     *
     * @return bool|array
     */
    public function getAccountList()
    {
        $this->throwAccessTokenError();
        $result = Tools::httpGet(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_KFLIST . "access_token={$this->access_token}");
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 获取多客服在线客服接待信息
     *
     * @return bool|array
     */
    public function getOnlineList()
    {
        $result = Tools::httpGet(self::API_URL_PREFIX . self::CUSTOM_SERVICE_GET_ONLINEKFLIST . "access_token={$this->access_token}");
        $result = $this->parsing($result);
        return $result;
    }

    /**
     * 邀请绑定客服帐号
     *
     * @return bool|array
     */
    public function inviteworker($data)
    {
        $this->throwAccessTokenError();
        $result = Tools::httpPost(self::API_BASE_URL_PREFIX . self::CS_KF_ACCOUNT_INVITEWORKER_URL . "access_token={$this->access_token}", Tools::json_encode($data));
        $result = $this->parsing($result);
        return $result;
    }

}

?>